package com.github.natholdallas.controller.copilot

import com.github.natholdallas.common.CViews
import com.github.natholdallas.common.LOGINID
import com.github.natholdallas.common.byId
import com.github.natholdallas.common.byIdThenUpdate
import com.github.natholdallas.controller.PageIn
import com.github.natholdallas.database.*
import com.github.natholdallas.declaration.Attr
import com.github.natholdallas.declaration.Body
import com.github.natholdallas.declaration.Path
import org.springframework.data.domain.Page
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PutMapping

@CViews("/user")
class UserCtl(private val repo: Users) {

    @[GetMapping]
    fun retrieve(): User {
        return repo.byId(LOGINID)
    }

    @[PutMapping]
    fun update(@Body data: UserIn) {
        repo.byIdThenUpdate(LOGINID, data)
    }

}


@CViews("/comment")
class CommentCtl(private val repo: Comments) {

    @[GetMapping]
    fun list(): List<CommentTable> {
        return repo.allByCopilot(LOGINID)
    }

}


@CViews("/order")
class OrderCtl(private val repo: Orders) {

    @[GetMapping]
    fun list(@Attr page: PageIn): Page<OrderTable> {
        return repo.allByCopilot(LOGINID, page)
    }

    @[GetMapping("/{id}")]
    fun retrieve(@Path id: Long): OrderTable {
        return repo.byId(id)
    }

}
